import { Button, ButtonProps } from "antd";
import { useEffect, useState } from "react";

export function CButton(props: ButtonProps) {
  const [loading, setLoading] = useState(props.loading);
  useEffect(() => {
    setLoading(props.loading);
  }, [props.loading]);
  return (
    <Button
      {...props}
      loading={loading}
      onClick={
        props.onClick
          ? (e) => {
              let res: any = props.onClick?.(e);
              if (res && typeof res.then == "function") {
                setLoading(true);
                Promise.resolve(res).finally(() => setLoading(false));
              }
            }
          : undefined
      }
    >
      {props.children}
    </Button>
  );
}

export default CButton;
